import API from '../../api/api.js'
import Dialog from '../../lib/vant-weapp/dialog/dialog';
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '我的文章'
    })
    let userInfo = wx.getStorageSync('userInfo')
    API.getWenZhangDataByOpenId(userInfo._openid).then(res => {
      
      this.setData({
        dataList: res
      })
    })
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  onDetailFunc: function (e) {
    let item = e.currentTarget.dataset.value
    wx.navigateTo({
      url: "/pages/wenzhangdetail/wenzhangdetail?id=" + item._id + "&mode=check"
    })
  },
  onDeleteWenZhangFunc: function (e) {
    Dialog.confirm({
        title: '删除文章？',
        message: '请谨慎确定是否删除此文章，此操作将彻底删除文章所有信息。',
      })
      .then(() => {
        wx.showLoading({
          title: '加载中...',
          mask: true
        })
        let userInfo = wx.getStorageSync('userInfo')
        let value = e.currentTarget.dataset.value
        let index = e.currentTarget.dataset.index

        API.removeWenZhang(value._id).then(res => {
          API.deleteImagesList(value.wenZhangImagesList)
          this.data.dataList.splice(index, 1)
          this.setData({
            dataList: this.data.dataList
          })
          wx.hideLoading()
          wx.showToast({
            title: '删除文章成功。',
            icon: "none"
          })
          wx.setStorageSync('userInfo', userInfo)
        })

      })
      .catch(() => {
        wx.showToast({
          title: '已取消删除笔友',
          icon: "none"
        })
      })

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let shareimages = app.globalData.shareimages
    let index = Math.floor(Math.random() * 5)
    return {
      title: '一个真正匿名的心情小程序',
      imageUrl: shareimages[index],
      path: '/pages/index/index'
    }
  },
  onShareTimeline: function () {
let shareimages = app.globalData.shareimages
    let index = Math.floor(Math.random() * 5)
    return {
      title: '一个真正匿名的心情小程序',
      imageUrl: shareimages[index]
    }
  }
})